package com.unittec.drainage.provider.utils;

import lombok.SneakyThrows;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.UUID;

/**
 * @author wuxinwei
 *
 */
public class OrderNumUtils {

    public static synchronized String createOrderNum() {
        int hashCodeV = UUID.randomUUID().toString().hashCode();
        if (hashCodeV < 0) {
            hashCodeV = -hashCodeV;
        }
        return getCurrentLocalTime("yyyyMMdd") + String.format("%010d", hashCodeV);
    }


    public static synchronized String createOrderNumByTime() {
        String beforTemp = "";
        String afterTemp = "";
        String finishTemp = "";
        beforTemp = getCurrentLocalTime("yyyyMMddHHmmssSSS");
        boolean flag = true;
        while (flag) {
            afterTemp = getCurrentLocalTime("yyyyMMddHHmmssSSS");
            if (!beforTemp.equalsIgnoreCase(afterTemp)) {
                flag = false;
                finishTemp = afterTemp;
            }
        }
        return finishTemp;
    }

    private static String getCurrentLocalTime(String format) {

        return formatDate(LocalDateTime.now(), format);
    }

    private static String formatDate(LocalDateTime date, String format) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(format);
        return formatter.format(date);
    }

}
